<?php

namespace app\admin\controller\admin;

use app\admin\controller\Controllers;
use app\admin\model\admin\Menus as MenusModel;
use app\exceptions\OrdinaryException;
use think\Db;
use app\admin\service\{
    Menus as MenusService,PermissionCategory
};

class Menus extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index( MenusService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\Menus.index');
        $list = $service->indexPaginate( $get );
        $config = config('field.menus');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'status' => $config['status']
        ]);
        return $this->fetch('admin/menus/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function create( PermissionCategory $permissionCategory )
    {
        $category = $permissionCategory->get([['status','=',1]]);
        $config = config('field.menus');
        $this->assign([
            'category' => $category,
            'status' => $config['status']
        ]);
        return $this->fetch('admin/menus/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function save(MenusService $menus)
    {
        $post = $this->request->only(['name','path','sort_num','level','category_id','status','icon'], 'post');
        $this->validate($post, 'app\admin\validate\Menus.create');
        Db::startTrans();
        $menuId = $menus->create($post);
        if ( false === $menuId ) {
            Db::rollback();
            return failed('新增失败');
        }
        if ( $post['level'] > 1 ) {
            try{
                $menus->relation($menuId, get($post, 'category_id', []));
            } catch ( OrdinaryException $exception ) {
                Db::rollback();
                return failed($exception->getMessage());
            }
        }
        Db::commit();
        return success();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function edit(int $id, MenusModel $menus, PermissionCategory $permissionCategory, MenusService $menusService)
    {
        $menuCategory = $menusService->getCategory($id);
        $config = config('field.menus');
        $category = $permissionCategory->get([['status','=',1]]);
        $this->assign([
            'data' => $menus,
            'status' => $config['status'],
            'category' => $category,
            'menuCategory' => $menuCategory
        ]);
        return $this->fetch('admin/menus/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id, MenusService $service)
    {
        $put = $this->request->only(['name','path','sort_num','category_id','status','level','icon'], 'put');
        $put['id'] = $id;
        $this->validate($put, 'app\admin\validate\Menus.update');
        Db::startTrans();
        $update = $service->save($id, $put);
        if ( !$update ) {
            Db::rollback();
            return failed('更新失败');
        }
        $category_id = get($put, 'category_id', []);
        if ( count($category_id) > 0 && $put['level'] > 1 ) {
            try{
                $service->relation($id, $category_id);
            } catch (OrdinaryException $exception) {
                Db::rollback();
                return failed('更新失败');
            }
        }
        Db::commit();
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, MenusService $service)
    {
        if ( $id <= 0 ) {
            return failed('参数错误,刷新重试!');
        }
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 检测菜单名是否唯一
     * @author zx
     * @date 2019-12-06
     * @return \think\Response
     */
    public function only( MenusService $service )
    {
        $get = $this->request->only(['id','name'], 'get');
        $this->validate($get, 'app\admin\validate\Menus.only');
        $bool = $service->only($get);
        return json($bool);
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-12-09
     * @return \think\Response
     */
    public function destory(MenusService $menus)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\Menus.destory');
        if ( $menus->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 状态修改
     *
     * @return \think\Response
     */
    public function status(MenusService $service, MenusModel $model)
    {
        return $service->status($model);
    }

}
